<?php
/**
 * @Author: 976123967@qq.com
 * @Date:   2014-09-04 21:29:38
 * @Last Modified by:   chen
 * @Last Modified time: 2014-09-04 21:30:01
 */
class Page {
     
    // 分页栏每页显示的页数
    public $rollPage = 7;
    // 页数跳转时要带的参数
    public $parameter  ;
    // 分页URL地址
    public $url     =   '';
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow    ;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页显示定制
    protected $config  =    array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>'<li class="thisclass"> %totalRow% %header% %nowPage%/%totalPage% 页</li> %first% %upPage% %linkPage% %prePage%  %downPage% %nextPage% %end%');
    // 默认分页变量名
    protected $varPage;
 
    /**
     * 架构函数
     * @access public
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     */
    public function __construct($totalRows,$listRows='',$parameter='',$url='',$pagelen = '11') {
         
        $this->totalRows    =   $totalRows;
        $this->parameter    =   $parameter;
        $this->pagelen    =   $pagelen;
         
        $this->varPage      =   C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this->listRows =   intval($listRows);
        }
        $this->totalPages   =   ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages    =   ceil($this->totalPages/$this->rollPage);
        $this->nowPage      =   !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
        if($this->nowPage<1){
            $this->nowPage  =   1;
        }elseif(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage  =   $this->totalPages;
        }
        $this->firstRow     =   $this->listRows*($this->nowPage-1);
        if(!empty($url))    $this->url  =   $url; 
    }
 
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }
 
    /**
     * 分页显示输出
     * @access public
     */
    public function show() {
        if(0 == $this->totalRows) return '';
        $p              =   $this->varPage;
        $nowCoolPage    =   ceil($this->nowPage/$this->rollPage);
 
        // 分析分页参数 PS:if判断为官方源码，具体有什么用还不清楚。
        if($this->url){
            $depr       =   C('URL_PATHINFO_DEPR');
            $url        =   rtrim(U('/'.$this->url,'',false),$depr).$depr.'__PAGE__';
        }else{
            if($this->parameter && is_string($this->parameter)) {
                parse_str($this->parameter,$parameter);
            }elseif(is_array($this->parameter)){
                $parameter      =   $this->parameter;
            }elseif(empty($this->parameter)){
                unset($_GET[C('VAR_URL_PARAMS')]);
                $var =  !empty($_POST)?$_POST:$_GET;
                if(empty($var)) {
                    $parameter  =   array();
                }else{
                    $parameter  =   $var;
                }
            }
            $parameter[$p]  =   '__PAGE__';
             
            $modlename = MODULE_NAME;
             
            /*         
            这儿比官方多了个判断
            第一是当处于搜索页面使用时，链接不一样，使用pathinfo模式的地址在Linux下会出现分页无法使用（获取关键字被转码）。具体为什么，不太清楚，win下面无异常
             
            第二是判断是否开启URL路由，开启路由分页的地址随着路由规则改变
             
            */
            if($modlename != 'Search'){
                if(URL_ROUTER_ON){
                    $url =  U('/'.$modlename.'/'.$parameter['id'].'/'.$parameter['p']); //配合路由规则生成URL
                }else{
                    $url = U('',$parameter);
                }
            }else{
                $keyword = str_replace(' ', '+', $parameter['q']);
                $url = '/Search/index.html?kwtype=1&q='.$keyword.'&searchtype=title&p=__PAGE__';
            }
            
        }
        //上下翻页字符串
        $upRow          =   $this->nowPage-1;
        $downRow        =   $this->nowPage+1;
        if ($upRow>0){
            $upPage     =   "<li><a href='".str_replace('__PAGE__',$upRow,$url)."'>".$this->config['prev']."</a></li>";
        }else{
            $upPage     =   "<li class=\"thisclass\">".$this->config['prev']."</li>";
        }
 
        if ($downRow <= $this->totalPages){
            $downPage   =   "<li><a href='".str_replace('__PAGE__',$downRow,$url)."'>".$this->config['next']."</a></li>";
        }else{
            $downPage   =   "<li class=\"thisclass\">".$this->config['next']."</li>";
        }
         
        $nowPageNo = $this->nowPage;
        $totalRows = $this->totalRows;
        $pagelen = $this->pagelen;
         
        if($nowPageNo<1) $nowPageNo = 1;
        if($nowPageNo>$totalRows) $nowPageNo = $totalRows;
        //计算查询偏移量
        $offset = $listnum*($nowPageNo-1);
        //页码范围计算
        $init = 1;//起始页码数
        $max = $totalRows;//结束页码数
        $pagelen = ($pagelen%2)?$pagelen:$pagelen+1;//页码个数
        $pageoffset = ($pagelen-1)/2;//页码个数左右偏移量
         
        //分页数大于页码个数时可以偏移
         
         
        if($totalRows > $pagelen){
            //如果当前页小于等于左偏移
            if($nowPageNo<=$pageoffset){
                $init=1;
                $max = $pagelen;
                 
            }else{//如果当前页大于左偏移
                //如果当前页码右偏移超出最大分页数
                if($nowPageNo+$pageoffset>=$totalRows+1){
                    $init = $totalRows-$pagelen+1;
                }else{
                    //左右偏移都存在时的计算
                    $init = $nowPageNo-$pageoffset;
                    $max = $nowPageNo+$pageoffset;
                }
            }
        }
         
        // 1 2 3 4 5
        $linkPage = "";
        for($i=$init;$i<=$max;$i++){
            $page = $i;
            if($page!=$this->nowPage){
                if($page<=$this->totalPages){
                    $linkPage .= "<li><a href='".str_replace('__PAGE__',$page,$url)."'>".$page."</a></li>";
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= "<li class=\"thisclass\"><span class='current'>".$page."</span></li>";
                }
            }
        }
        $pageStr     =   str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%first%','%upPage%','%linkPage%','%prePage%','%downPage%','%nextPage%','%end%'),
            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$theFirst,$upPage,$linkPage,$prePage,$downPage,$nextPage,$theEnd),$this->config['theme']);
        return $pageStr;
    }
 
}